using UnityEngine;

[ExecuteInEditMode]
public class ScreenBlurEffect : MonoBehaviour
{
    // 引用着色器材质
    public Material blurMaterial;
    
    // 模糊半径控制参数
    [Range(0, 1f)]
    public float blurRadius = 0f;
    
    // 动画控制参数
    [Header("Animation Settings")]
    public bool animateBlur = false;
    public float animationSpeed = 1f;
    public float maxBlur = 0.05f;
    
    void Start()
    {
        // 如果没有指定材质，则尝试获取组件上的材质
        if (blurMaterial == null)
        {
            Renderer renderer = GetComponent<Renderer>();
            if (renderer != null)
            {
                blurMaterial = renderer.sharedMaterial;
            }
        }
    }
    
    void Update()
    {
        // 更新材质中的模糊半径参数
        if (blurMaterial != null)
        {
            blurMaterial.SetFloat("_BlurStrength", blurRadius);
        }
        
        // 如果启用动画，则自动变化模糊值
        if (animateBlur)
        {
            blurRadius = Mathf.PingPong(Time.time * animationSpeed, maxBlur);
        }
    }
    
    // 后处理渲染函数
    void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        if (blurMaterial != null)
        {
            Graphics.Blit(source, destination, blurMaterial);
        }
        else
        {
            Graphics.Blit(source, destination);
        }
    }
}
